//
//  BYWebView.h
//  Unity-iPhone
//
//  Created by 胡忠诚 on 2018/4/26.
//通用的WebView

#import <UIKit/UIKit.h>
#import <WebKit/WKScriptMessageHandler.h>

//========================BYWKWebView代理对外部的回调（WK和UIWebView）====================
@class BYWebView;
@protocol BYWebViewDelegate <NSObject>
@optional
- (void)webViewDidStartLoad:(BYWebView *)webView;
- (void)webViewDidFinishLoad:(BYWebView *)webView;
- (void)webView:(BYWebView *)webView didFailLoadWithError:(NSError *)error;
- (BOOL)webView:(BYWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
- (void)backBtnDelegateClick;
@end
@interface BYWebView : UIView
/**BYWKWebView对外部的代理*/
@property(weak,nonatomic)id<BYWebViewDelegate> delegate;
/**内部使用的webView：UI和WKWebView都赋值给它*/
@property (nonatomic, readonly) id realWebView;
/**是否使用UIWebView：YES是、NO是WKWebView*/
@property (nonatomic, readonly) BOOL usingUIWebView;
/**是否根据视图大小来缩放页面：默认为YES*/
@property (nonatomic) BOOL scalesPageToFit;
//加载网页
- (id)loadRequest:(NSURLRequest *)request;

//========================其他====================
@property (nonatomic, readonly) UIScrollView *scrollView;
/**网页URL*/
@property (nonatomic, readonly) NSURL *URL;
/**网页的标题（WK、UI)*/
@property (nonatomic, copy) NSString *title;
/**是否加载中*/
@property (nonatomic, readonly, getter=isLoading) BOOL loading;
/**是否可返回*/
@property (nonatomic, readonly) BOOL canGoBack;
/**是否可前进*/
@property (nonatomic, readonly) BOOL canGoForward;
@property (nonatomic) UIButton *backbtn;

- (id)goBack;//后退
- (id)goForward;//前进
- (id)reload;//刷新
- (id)reloadFromOrigin;//加载原始请求
- (void)stopLoading;//停止加载
- (id)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;//加载HTML
- (NSInteger)countOfHistory;//获取当前的网页层级数
- (void)gobackWithStep:(NSInteger)step;//返回到第几层

//========================JS相关======================
//添加js回调oc通知方式，适用于 iOS8 之后
- (void)addScriptMessageHandler:(id <WKScriptMessageHandler>)scriptMessageHandler name:(NSString *)name;
//注销 注册过的js回调oc通知方式，适用于 iOS8 之后
- (void)removeScriptMessageHandlerForName:(NSString *)name;
//oc调用js代码执行方法
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^)(id, NSError *))completionHandler;
//不建议使用这个办法  因为会在内部等待webView 的执行结果
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)javaScriptString __deprecated_msg("Method deprecated. Use [evaluateJavaScript:completionHandler:]");

@end
